package server;

import java.io.IOException;
import java.io.InputStream;

public class Request {
    private InputStream input;
    private String uri;
    private String request;
    public Request(InputStream input) {
        this.input = input;
    }
    public void parse() {
        StringBuffer request = new StringBuffer(2048);
        byte[] buffer = new byte[2048];
        int len;
        try {
            len = input.read(buffer);
        } catch (IOException e) {
            e.printStackTrace();
            len = -1;
        }
        for (int i = 0; i < len; i++) {
            request.append((char)buffer[i]);
        }
        this.request = request.toString();
        parseUri();
    }
    private void parseUri() {
        String request = this.request;
        int beginIndex = request.indexOf(' ');
        if (beginIndex != -1) {
            int endIndex = request.indexOf(' ', beginIndex + 1);
            if (endIndex > beginIndex) {
                this.uri = request.substring(beginIndex + 1, endIndex);
            }
        }
    }
    public String getUri() {
        return this.uri;
    }
    public String getRequest() {
        return  this.request;
    }
}
